package day02;

import java.util.LinkedList;

public class test02 {
    public static int[] findMax(int[] num,int k) {
        if(num == null || k < 1 || k > num.length) {
            return null;
        }

        LinkedList<Integer> linkedList = new LinkedList<>();
        int[] res = new int[num.length-k+1];
        int index = 0;
        for (int i = 0; i < num.length ;i++) {
            while (!linkedList.isEmpty() && num[linkedList.getLast()] <= num[i]) {
                linkedList.removeLast();
            }
            linkedList.addLast(i);
            if(linkedList.getFirst() == i-k){
                linkedList.removeFirst();
            }
            if(i >= k-1) {
                res[index++] = num[linkedList.getFirst()];
            }

        }
        return res;
    }
}
